package com.example.myapplication.shortvideo.utils;

import android.text.TextUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 用来计算显示的时间是多久之前的！
 * Created by Circle on 2018/11/1
 */
public class FormatTimeUtil {

    public static SimpleDateFormat hmFormat = new SimpleDateFormat("HH:mm");
    public static SimpleDateFormat ymdFormat = new SimpleDateFormat("yyyy/MM/dd");
    public static SimpleDateFormat ymdhmFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    private FormatTimeUtil() {
    }

    public static String getConversationListTime(long time) {
        if (DateUtils.isSameDay(time)) {
            return hmFormat.format(time);
        } else {
            return ymdFormat.format(time);
        }
    }

    public static String getConversationTime(long time) {
        if (DateUtils.isSameDay(time)) {
            return hmFormat.format(time);
        } else {
            return ymdhmFormat.format(time);
        }
    }


    public static String getPublishTime(long l) {
        return getPublishTime(l, System.currentTimeMillis());
    }

    public static String getTime(long time, SimpleDateFormat simpleDateFormat) {
        return simpleDateFormat.format(time);
    }

    public static String getPublishTime(long l, long current) {
        String msg = "";
        if (DateUtils.isSameDay(l)) {
            long time = (current - l) / 1000;
            if (time / 60 == 0) {
                msg = "1分钟前";
            } else if (time / 60 > 0 && time / 3600 == 0) {
                msg = time / 60 + "分钟前";
            } else if (time / 3600 > 0 && time / (3600 * 24) == 0) {
                msg = time / 3600 + "小时前";
            }
        } else if (DateUtils.isYesterday(l)) {
            msg = "昨天";
        } else {
            msg = formatTimeMillis(l, "MM-dd HH:mm:ss");
        }
        return msg;
    }

    public static String formatTimeMillis(long ms, String format) {
        Date date = new Date(ms);
        if (TextUtils.isEmpty(format)) {
            format = "M月d日";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * 格式化友好的时间差显示方式
     *
     * @param millis 开始时间戳
     * @return
     */
    public static String getTimeSpanByNow1(long millis) {
        long now = System.currentTimeMillis();
        long span = now - millis;
        if (span < 1000) {
            return "刚刚";
        } else if (span < DateUtils.MIN) {
            return String.format("%d秒前", span / DateUtils.SEC);
        } else if (span < DateUtils.HOUR) {
            return String.format("%d分钟前", span / DateUtils.MIN);
        } else if (span < DateUtils.DAY) {
            return String.format("%d小时前", span / DateUtils.HOUR);
        } else if (span < DateUtils.WEEK) {
            return String.format("%d天前", span / DateUtils.DAY);
        } else if (span < DateUtils.MONTH) {
            return String.format("%d周前", span / DateUtils.WEEK);
        } else if (span < DateUtils.YEAR) {
            return String.format("%d月前", span / DateUtils.MONTH);
        } else {
            return String.format("%d年前", span / DateUtils.YEAR);
        }
    }

    /**
     * 格式化友好的时间差显示方式
     *
     * @param millis 开始时间戳
     * @return
     */
    public static String getTimeSpanByNow2(long millis) {
        long now = System.currentTimeMillis();
        long span = now - millis;
        long day = span / DateUtils.DAY;
        if (day == 0) {// 今天
            long hour = span / DateUtils.HOUR;
            if (hour <= 4) {
                return String.format("凌晨%tR", millis);
            } else if (hour > 4 && hour <= 6) {
                return String.format("早上%tR", millis);
            } else if (hour > 6 && hour <= 11) {
                return String.format("上午%tR", millis);
            } else if (hour > 11 && hour <= 13) {
                return String.format("中午%tR", millis);
            } else if (hour > 13 && hour <= 18) {
                return String.format("下午%tR", millis);
            } else if (hour > 18 && hour <= 19) {
                return String.format("傍晚%tR", millis);
            } else if (hour > 19 && hour <= 24) {
                return String.format("晚上%tR", millis);
            } else {
                return String.format("今天%tR", millis);
            }
        } else if (day == 1) {// 昨天
            return String.format("昨天%tR", millis);
        } else if (day == 2) {// 前天
            return String.format("前天%tR", millis);
        } else {
            return String.format("%tF", millis);
        }
    }

    public static String getVideoTime(int duration) {
        int totalSeconds = duration / 1000;
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;
        if (hours > 0) {
            return String.format("%d:%02d:%02d", hours, minutes, seconds);
        } else {
            return String.format("%02d:%02d", minutes, seconds);
        }
    }

    /**
     * 格式化s
     *
     * @param mills
     * @return
     */
    public static int formatSecond(long mills) {
        return (int) Math.ceil(mills / 1000.0);
    }

    public static String formatTaskCountTime(long mills) {
        int second = formatSecond(mills);
        int hour = second / 60;
        second = second % 60;
        String result = "剩余";
        if (hour > 0) {
            result += hour + "分";
        }

        result += second + "秒";
        return result;
    }

    public static String convertTodayTime(long milliseconds) {
        if (DateUtils.isSameDay(milliseconds)) {// 今天
            return "今天 " + getTime(milliseconds, hmFormat);
        } else {
            return getTime(milliseconds, ymdhmFormat);
        }
    }
}
